package com.cloudm.technician.ui.modules.cloudm_box.machine_list

import android.view.View
import android.view.ViewGroup
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.extensions.inflate
import com.cloudm.technician.ui.base.BaseAdapter
import com.cloudm.technician.ui.base.BaseViewHolder
import com.cloudm.technician.ui.widget.OnceClickListener
import kotlinx.android.synthetic.main.item_machine_list.view.*

/**
 * 作者: wesley
 * 时间: 2017/12/18
 * 描述: 机器列表适配器
 */
class MachineListAdapter : BaseAdapter<DeviceInfo>() {

    var listener: OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): BaseViewHolder<DeviceInfo> {
        return ItemViewHolder(parent?.inflate(R.layout.item_machine_list))
    }

    private inner class ItemViewHolder(itemView: View?) : BaseViewHolder<DeviceInfo>(itemView) {

        override fun onBind(t: DeviceInfo) {
            itemView.deviceName.text = t.deviceName
            itemView.createTime.text = t.createTime
            when (t.status) {
                2 -> itemView.machineStatus.text = itemView.context.resources.getString(
                        R.string.cloudm_debug)
                3 -> itemView.machineStatus.text = itemView.context.resources.getString(
                        R.string.cloudm_pic)
                5 -> itemView.machineStatus.text = itemView.context.resources.getString(
                        R.string.level_debug)
                6 -> itemView.machineStatus.text = itemView.context.resources.getString(
                        R.string.level_pic)
                7 -> itemView.machineStatus.text = itemView.context.resources.getString(
                        R.string.transfer)
                8 -> itemView.machineStatus.text = itemView.context.resources.getString(
                        R.string.wait_pay)
                9 -> itemView.machineStatus.text = itemView.context.resources.getString(
                        R.string.debug_done)
                else -> itemView.machineStatus.text = itemView.context.resources.getString(
                        R.string.unknow_status)
            }

            itemView.machineStatus.setOnClickListener(object : OnceClickListener() {
                override fun onOnceClick(view: View) {
                    listener?.onMachineStatusClick(t)
                }
            })
        }
    }


    interface OnItemClickListener {

        fun onMachineStatusClick(info: DeviceInfo)
    }
}